package org.example.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx, Environment env) {
        return args -> {
            // 打印精美 Banner
            printStartupBanner();
            
            // 打印环境信息
            printEnvironmentInfo(env);
            
            // 打印使用的第三方框架
            printThirdPartyFrameworks();
        };
    }

    private void printStartupBanner() {
        System.out.println("\n");
        System.out.println("███████╗████████╗ █████╗ ██████╗ ███████╗    ███████╗██╗   ██╗ ██████╗███████╗███████╗");
        System.out.println("██╔════╝╚══██╔══╝██╔══██╗██╔══██╗██╔════╝    ██╔════╝██║   ██║██╔════╝██╔════╝██╔════╝");
        System.out.println("███████╗   ██║   ███████║██████╔╝█████╗      ███████╗██║   ██║██║     █████╗  ███████╗");
        System.out.println("╚════██║   ██║   ██╔══██║██╔══██╗██╔══╝      ╚════██║██║   ██║██║     ██╔══╝  ╚════██║");
        System.out.println("███████║   ██║   ██║  ██║██║  ██║███████╗    ███████║╚██████╔╝╚██████╗███████╗███████║");
        System.out.println("╚══════╝   ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝    ╚══════╝ ╚═════╝  ╚═════╝╚══════╝╚══════╝");
        System.out.println("\n");
        System.out.println("🚀 Application Started Successfully!");
        System.out.println("======================================");
    }

    private void printEnvironmentInfo(Environment env) {
        String port = env.getProperty("server.port", "8080");
        String contextPath = env.getProperty("server.servlet.context-path", "/");
        String profiles = Arrays.toString(env.getActiveProfiles());
        
        if (profiles.isEmpty()) {
            profiles = "[default]";
        }
        
        System.out.println("\n🌐 Environment Information:");
        System.out.println("--------------------------------------");
        System.out.printf("➤ Port: %s\n", port);
        System.out.printf("➤ Context Path: %s\n", contextPath);
        System.out.printf("➤ Active Profiles: %s\n", profiles);
        System.out.printf("➤ Java Version: %s\n", System.getProperty("java.version"));
        System.out.println("--------------------------------------");
    }

    private void printThirdPartyFrameworks() {
        System.out.println("\n🔧 Third-Party Frameworks:");
        System.out.println("--------------------------------------");
        
        // 检测常用框架
        detectFramework("Spring Boot", "org.springframework.boot.SpringApplication");
        detectFramework("Spring MVC", "org.springframework.web.servlet.DispatcherServlet");
        detectFramework("Spring Data JPA", "org.springframework.data.jpa.repository.JpaRepository");
        detectFramework("MyBatis", "org.apache.ibatis.session.SqlSession");
        detectFramework("Hibernate", "org.hibernate.Session");
        detectFramework("Redis", "org.springframework.data.redis.core.RedisTemplate");
        detectFramework("MongoDB", "org.springframework.data.mongodb.core.MongoTemplate");
        detectFramework("RabbitMQ", "org.springframework.amqp.rabbit.core.RabbitTemplate");
        detectFramework("Kafka", "org.springframework.kafka.core.KafkaTemplate");
        detectFramework("XXL-Job", "com.xxl.job.core.executor.XxlJobExecutor");
        detectFramework("Swagger", "io.swagger.v3.oas.annotations.Operation");
        detectFramework("Lombok", "lombok.extern.slf4j.Slf4j");
        detectFramework("Logback", "ch.qos.logback.classic.Logger");
        detectFramework("SLF4J", "org.slf4j.Logger");
        
        System.out.println("--------------------------------------");
    }

    private void detectFramework(String frameworkName, String className) {
        try {
            Class.forName(className);
            System.out.printf("✓ %s\n", frameworkName);
        } catch (ClassNotFoundException e) {
            // 框架未使用，不打印
        }
    }
}